/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ResourceProviderPanel.java
 *
 * Created on Sep 16, 2011, 6:46:18 PM
 */

package com.agentslab.resourceprovider;

import aa.core.ActorMessage;
import aa.core.Contract;
import aa.core.Platform;
import aa.tool.CyberOrgTuple;
import com.agentslab.appstarter.MainFrame;
import com.agentslab.constants.APIConstants;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.JFrame;

/**
 *
 * @author mehadihasan
 */
public class ResourceProviderPanel extends javax.swing.JPanel {

    /** Creates new form ResourceProviderPanel */
    public ResourceProviderPanel() {
        this.platform = APIConstants.platform;
        this.setProposedContract();
        initComponents();
    }

    public Contract getProposedContract() {

        try{
            this.proposedContract = new Contract(Integer.parseInt(this.nTicksTextField.getText()), Integer.parseInt(this.tickRateTextField.getText()), Integer.parseInt(this.priceTextField.getText()));
        } catch(Exception e){
            this.setProposedContract();
        }
        return proposedContract;
    }

    public void setProposedContract() {
        this.proposedContract = new Contract(new Long(90000000),new Long(90000),new Long(1000));
    }

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        ipTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        idTextField = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        nTicksTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        tickRateTextField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        priceTextField = new javax.swing.JTextField();
        okButton = new javax.swing.JButton();

        jLabel1.setText("Please enter the IP and ID of the resource");

        try{
            byte[] ipAddr = InetAddress.getLocalHost().getAddress();

            // Convert to dot representation
            String ipAddrStr = "";
            for (int i=0; i<ipAddr.length; i++) {
                if (i > 0) {
                    ipAddrStr += ".";
                }
                ipAddrStr += ipAddr[i]&0xFF;
            }
            ipTextField.setText(ipAddrStr);
        } catch (UnknownHostException e) {

        }

        jLabel2.setText("IP address: ");

        jLabel3.setText("ID");

        idTextField.setText("2");

        jLabel4.setText("Proposed contract: ");

        jLabel5.setText("# of ticks required: ");

        nTicksTextField.setText("90000000");

        jLabel6.setText("Ticks rate: ");

        tickRateTextField.setText("90000");

        jLabel7.setText("Proposed price:");

        priceTextField.setText("1000");

        okButton.setText("Ok");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel1))
                    .add(layout.createSequentialGroup()
                        .add(22, 22, 22)
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(ipTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 128, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(idTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 48, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel4))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel5)
                            .add(jLabel6)
                            .add(jLabel7))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(nTicksTextField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                            .add(tickRateTextField)
                            .add(priceTextField))))
                .add(129, 129, 129))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                    .add(okButton))
                .add(100, 100, 100))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(ipTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2)
                    .add(jLabel3)
                    .add(idTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 12, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(nTicksTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(tickRateTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel7)
                    .add(priceTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(okButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        // TODO add your handling code here:
        CyberOrgTuple cyberOrgTuple=new CyberOrgTuple(platform.getActorNameOfPlatform().getHostAddress(), 2, "aa.drivers.FaciPara", this.getProposedContract());
        //Object[] argsreg=new Object[1];
        argsreg[0]=cyberOrgTuple;
        argsreg[1] = APIConstants.guiActor;
        argsreg[2] = ""+System.currentTimeMillis();
        ActorMessage am_reg = new ActorMessage(platform.getActorNameOfPlatform(),
                                               APIConstants.resourceBroker,
                                               "registerToCDM",
                                               argsreg,
                                               false
                                               );

        platform.sendMessage(am_reg);
    }//GEN-LAST:event_okButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField idTextField;
    private javax.swing.JTextField ipTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField nTicksTextField;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField priceTextField;
    private javax.swing.JTextField tickRateTextField;
    // End of variables declaration//GEN-END:variables
    private final Platform platform;
    private Contract proposedContract;
    private Object[] argsreg=new Object[3];

}
